package com.zzq.provider.service.impl;

import com.zzq.provider.bean.Student;
import com.zzq.provider.repository.StudentRepository;
import com.zzq.provider.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @Author zzq
 * @Date 2020/2/26 18:37
 * @Version 1.0
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentRepository repository;

    @Override
    public boolean saveStudent(Student student) {
        return repository.save(student) != null;
    }

    @Override
    public boolean deleteStudent(Integer id) {
        if (repository.existsById(id)) {
            repository.deleteById(id);
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStudent(Student student) {
        return repository.save(student) != null;
    }

    @Override
    public Student getStudent(Integer id) {
        if (repository.existsById(id)) {
            return repository.getOne(id);
        } else {
            Student student = new Student();
            student.setName("test");
            return student;
        }
    }

    @Override
    public Optional<Student> getById(Integer id) {
        return repository.findById(id);
    }

    @Override
    public List<Student> findAll() {
        return repository.findAll();
    }
}
